package com.hotelManager.www.controller;

import com.hotelManager.www.po.User;
import com.hotelManager.www.po.Shop;
import com.hotelManager.www.po.RoomType;
import com.hotelManager.www.service.UserService;
import com.hotelManager.www.service.ShopService;
import com.hotelManager.www.service.RoomTypeService;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class TouristController {
    private final UserService userService = new UserService();
    private final ShopService shopService = new ShopService();
    private final RoomTypeService roomTypeService = new RoomTypeService();

    // 注册功能
    public boolean register(User user, Shop shop) {
        if ("merchant".equals(user.getRole())) {
            return userService.registerMerchantUser(user) && userService.registerMerchantShop(user, shop);
        } else {
            return userService.registerUser(user);
        }
    }

    // 浏览店铺列表（分页）
    // 浏览店铺列表（分页）
    public Map<String, Object> browseShops(int page, int pageSize) {
        List<Shop> list = shopService.searchShopsWithPagination("", page, pageSize);
        int total = shopService.countVisibleShopsByKeyword("");
        int totalPages = (int) Math.ceil((double) total / pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("totalPages", totalPages);
        result.put("page", page);
        result.put("pageSize", pageSize);
        return result;
    }

    // 搜索店铺（分页）
    public Map<String, Object> searchShops(String keyword, int page, int pageSize) {
        List<Shop> list = shopService.searchShopsWithPagination(keyword, page, pageSize);
        int total = shopService.countVisibleShopsByKeyword(keyword);
        int totalPages = (int) Math.ceil((double) total / pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("totalPages", totalPages);
        result.put("page", page);
        result.put("pageSize", pageSize);
        return result;
    }

    // 获取店铺详情（同时更新浏览量）
    public Shop getShopDetail(int shopId) {
        Shop shop = shopService.getShopBriefById(shopId);
        if (shop != null) {
            // 更新浏览量
            shopService.updateViewCount(shopId);
        }
        return shop;
    }

    // 获取店铺房型列表
    public List<RoomType> getShopRoomTypes(int shopId) {
        return roomTypeService.getRoomTypesByShopId(shopId);
    }
}